package com.ibest.experience.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ibest.framework.common.enums.EnumsRtnMapResult;
import com.ibest.framework.common.persistence.BaseController;
import com.ibest.framework.common.utils.PageList;
import com.ibest.framework.common.utils.UserUtils;
import com.ibest.utils.ConstantUtils;
import com.ibest.utils.RandomUtils;
import com.ibest.accesssystem.entity.AccessSystem;
import com.ibest.accesssystem.service.AccessSystemService;
import com.ibest.experience.dto.input.ExperienceGradeInputDTO;
import com.ibest.experience.entity.ExperienceGrade;
import com.ibest.experience.service.ExperienceGradeService;

@Controller
@RequestMapping(value="${adminPath}/experience/experienceGrade")
public class ExperienceGradeController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(ExperienceGradeController.class);
	
	@Autowired
	private ExperienceGradeService experienceGradeService;
	
	@Autowired
	private AccessSystemService accessSystem;
	/**
	* 进入到列表页
	*/
	@RequestMapping(value="/")
	public String index(){
		return "module/experience/experienceGrade/experienceGradeList";
	}
	
	/**
	* 进入到表单页-创建
	*/
	@RequestMapping(value="/add")
	public String add(){
		
		return "module/experience/experienceGrade/experienceGradeForm";
	}
	
	/**
	* 进入到表单页，编辑
	*/
	@RequestMapping(value="/edit")
	public String edit(@RequestParam String id, Model model){
		try {
			if(StringUtils.isNotEmpty(id)){
				ExperienceGrade experienceGrade = experienceGradeService.findById(id);
				if(experienceGrade != null){
					model.addAttribute("experienceGrade", experienceGrade);
				}else{
					setRtnCodeAndMsg(EnumsRtnMapResult.FAILURE.getCode(), "您查看的信息不存在！", model);
				}
			}else{
				setRtnCodeAndMsg(EnumsRtnMapResult.FAILURE.getCode(), "请选择需要编辑的信息！", model);
			}
		} catch (Exception e) {
			setRtnCodeAndMsg(EnumsRtnMapResult.EXCEPTION.getCode(), EnumsRtnMapResult.EXCEPTION.getMsg(), model);
		}
		return "module/experience/experienceGrade/experienceGradeForm";
	}
	
	/**
	* 进入到详情页
	*/
	@RequestMapping(value="/view")
	public String view(@RequestParam String id, Model model){
		try {
			if(StringUtils.isNotEmpty(id)){
				ExperienceGrade experienceGrade = experienceGradeService.findById(id);
				if(experienceGrade != null){
					/*AccessSystem system = accessSystem.findById(experienceGrade.getSystemId());
					model.addAttribute("systemName",system.getName());*/
					model.addAttribute("experienceGrade", experienceGrade);
				}else{
					setRtnCodeAndMsg(EnumsRtnMapResult.FAILURE.getCode(), "您查看的信息不存在！", model);
				}
			}else{
				setRtnCodeAndMsg(EnumsRtnMapResult.FAILURE.getCode(), "请选择需要查看的信息！", model);
			}
		} catch (Exception e) {
			setRtnCodeAndMsg(EnumsRtnMapResult.EXCEPTION.getCode(), EnumsRtnMapResult.EXCEPTION.getMsg(), model);
		}
		return "module/experience/experienceGrade/experienceGradeDetail";
	}

	/**
	* 异步分页查询
	*/
	@ResponseBody
	@RequiresPermissions("experienceGrade:query")
	@RequestMapping(value="/list")
	public PageList<ExperienceGrade> list(ExperienceGradeInputDTO experienceGrade, HttpServletRequest request){
		
		PageList<ExperienceGrade> pageList = new PageList<ExperienceGrade>();
		
		try {
			//设置分页参数
			super.setPage(request, pageList);
		
			pageList = experienceGradeService.findByPage(pageList, experienceGrade);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageList;
	}
	
	/**
	* 异步表单提交
	*/
	@ResponseBody
	@RequiresPermissions("experienceGrade:create")
	@RequestMapping(value="create")
	public Map<String, Object> insert(ExperienceGrade experienceGrade){

		Map<String, Object> rtnMap = new HashMap<String, Object>();
		setRtnCodeAndMsgBySuccess(rtnMap, "保存成功");
		
		try {
			ExperienceGrade unique = 
					experienceGradeService.checkNameUnique(experienceGrade.getName());
			if(unique!=null){
				setRtnCodeAndMsgByFailure(rtnMap, "保存失败,与其它等级名称重复!");
			}else{
				
				 ExperienceGradeInputDTO inputDto = new ExperienceGradeInputDTO();
				 List<ExperienceGrade> findByObject = experienceGradeService.findByObject(inputDto);
				 if(findByObject != null && findByObject.size()==0){
					 if(experienceGrade.getPreExp()!=0){
						 setRtnCodeAndMsgByFailure(rtnMap, "保存失败,经验起始值必须为0");
					 }else{
						 	experienceGrade.setIsDelete(ConstantUtils.UPDATE_SET_ZERO);
							experienceGrade.setId(RandomUtils.RandomUUID());
							experienceGrade.setCreater(UserUtils.getCurrentUser().getRealname());
							experienceGrade.setCreateTime(new Date());
							experienceGrade.setModifier(UserUtils.getCurrentUser().getRealname());
							experienceGrade.setModifyTime(new Date());
							int result = experienceGradeService.insert(experienceGrade);
							if(result == 0){
								setRtnCodeAndMsgByFailure(rtnMap, "保存失败");
							}
					 }
				 }else{
					 int max =experienceGradeService.queryMaxNum()+1;
					 if(experienceGrade.getPreExp()!=max){
						 setRtnCodeAndMsgByFailure(rtnMap, "经验左区间起始值必须为:"+max);
					 }else{
						 experienceGrade.setIsDelete(ConstantUtils.UPDATE_SET_ZERO);
							experienceGrade.setId(RandomUtils.RandomUUID());
							experienceGrade.setCreater(UserUtils.getCurrentUser().getRealname());
							experienceGrade.setCreateTime(new Date());
							experienceGrade.setModifier(UserUtils.getCurrentUser().getRealname());
							experienceGrade.setModifyTime(new Date());
							int result = experienceGradeService.insert(experienceGrade);
							if(result == 0){
								setRtnCodeAndMsgByFailure(rtnMap, "保存失败");
							}
					 }
				 }
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			setRtnCodeAndMsgByException(rtnMap, null);
		}
		return rtnMap;
	}
	
	@ResponseBody
	@RequiresPermissions("experienceGrade:update")
	@RequestMapping(value="update")
	public Map<String, Object> update(ExperienceGrade experienceGrade){

		Map<String, Object> rtnMap = new HashMap<String, Object>();
		setRtnCodeAndMsgBySuccess(rtnMap, "保存成功");
		
		try {
			String oldName=experienceGradeService.findById(experienceGrade.getId()).getName();
			if(!oldName.equals(experienceGrade.getName())){
				ExperienceGrade unique = 
						experienceGradeService.checkNameUnique(experienceGrade.getName());
				if(unique!=null){
					setRtnCodeAndMsgByFailure(rtnMap, "保存失败,与其它等级名称重复!");
				}else{
					experienceGrade.setModifier(UserUtils.getCurrentUser().getRealname());
					experienceGrade.setModifyTime(new Date());
					int result = experienceGradeService.update(experienceGrade);
					if(result == 0){
						setRtnCodeAndMsgByFailure(rtnMap, "保存失败");
					}
				}
			}else{
				experienceGrade.setModifier(UserUtils.getCurrentUser().getRealname());
				experienceGrade.setModifyTime(new Date());
				int result = experienceGradeService.update(experienceGrade);
				if(result == 0){
					setRtnCodeAndMsgByFailure(rtnMap, "保存失败");
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			setRtnCodeAndMsgByException(rtnMap, null);
		}
		return rtnMap;
	}
	
	@ResponseBody
	@RequiresPermissions("experienceGrade:delete")
	@RequestMapping(value="delete")
	public Map<String, Object> delete(@RequestParam(required=true) String ids){
		
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		setRtnCodeAndMsgBySuccess(rtnMap, "删除成功");
		
		try {
			int result = experienceGradeService.deleteByIds(ids);
			if(result == 0){
				setRtnCodeAndMsgByFailure(rtnMap, "删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			setRtnCodeAndMsgByException(rtnMap, null);
		}
		return rtnMap;
	}
}
